<?php

/**
 * This is the model class for table "sub_feature_parm".
 *
 * The followings are the available columns in table 'sub_feature_parm':
 * @property string $MAIN_NUMBER
 * @property integer $FEATURE
 * @property integer $ACTION
 * @property integer $F_PARM
 * @property integer $TIME_ROUTE
 * @property string $F_NUMBER
 * @property string $RESERVED
 * @property string $USER_DEFINED
 * @property string $ENTITY_ID
 * @property integer $USER_DOMAIN
 * @property integer $READ_LEVEL
 * @property integer $WRITE_LEVEL
 * @property string $DATA_VERSION
 * @property integer $DATA_STATUS
 * @property integer $LAST_TRANSACT
 * @property string $LAST_MODIFIED
 * @property string $MODIFIED_DATE
 */
class SUBFEATUREPARM extends EnticeActiveRecord
{
	public $country_code;
	public $phone_number;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return SUBFEATUREPARM the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'SUB_FEATURE_PARM';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('MAIN_NUMBER, FEATURE, F_PARM, country_code, phone_number', 'required'),
			array('FEATURE, ACTION, F_PARM, TIME_ROUTE, USER_DOMAIN, READ_LEVEL, WRITE_LEVEL, DATA_STATUS, LAST_TRANSACT', 'numerical', 'integerOnly'=>true),
			array('MAIN_NUMBER, F_NUMBER', 'length', 'max'=>32),
			array('RESERVED, USER_DEFINED', 'length', 'max'=>12),
			array('ENTITY_ID, LAST_MODIFIED', 'length', 'max'=>20),
			array('DATA_VERSION', 'length', 'max'=>15),
			array('MODIFIED_DATE, country_code, phone_number', 'safe'),
			array('F_NUMBER', 'uniqueSpeedDial', 'on'=>'add'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('MAIN_NUMBER, FEATURE, ACTION, F_PARM, TIME_ROUTE, F_NUMBER, RESERVED, USER_DEFINED, ENTITY_ID, USER_DOMAIN, READ_LEVEL, WRITE_LEVEL, DATA_VERSION, DATA_STATUS, LAST_TRANSACT, LAST_MODIFIED, MODIFIED_DATE', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'MAIN_NUMBER' => 'Main Number',
			'FEATURE' => 'Feature',
			'ACTION' => 'Action',
			'F_PARM' => 'F Parm',
			'TIME_ROUTE' => 'Time Route',
			'F_NUMBER' => 'F Number',
			'RESERVED' => 'Reserved',
			'USER_DEFINED' => 'User Defined',
			'ENTITY_ID' => 'Entity',
			'USER_DOMAIN' => 'User Domain',
			'READ_LEVEL' => 'Read Level',
			'WRITE_LEVEL' => 'Write Level',
			'DATA_VERSION' => 'Data Version',
			'DATA_STATUS' => 'Data Status',
			'LAST_TRANSACT' => 'Last Transact',
			'LAST_MODIFIED' => 'Last Modified',
			'MODIFIED_DATE' => 'Modified Date',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('MAIN_NUMBER',$this->MAIN_NUMBER,true);
		$criteria->compare('FEATURE',$this->FEATURE);
		$criteria->compare('ACTION',$this->ACTION);
		$criteria->compare('F_PARM',$this->F_PARM);
		$criteria->compare('TIME_ROUTE',$this->TIME_ROUTE);
		$criteria->compare('F_NUMBER',$this->F_NUMBER,true);
		$criteria->compare('RESERVED',$this->RESERVED,true);
		$criteria->compare('USER_DEFINED',$this->USER_DEFINED,true);
		$criteria->compare('ENTITY_ID',$this->ENTITY_ID,true);
		$criteria->compare('USER_DOMAIN',$this->USER_DOMAIN);
		$criteria->compare('READ_LEVEL',$this->READ_LEVEL);
		$criteria->compare('WRITE_LEVEL',$this->WRITE_LEVEL);
		$criteria->compare('DATA_VERSION',$this->DATA_VERSION,true);
		$criteria->compare('DATA_STATUS',$this->DATA_STATUS);
		$criteria->compare('LAST_TRANSACT',$this->LAST_TRANSACT);
		$criteria->compare('LAST_MODIFIED',$this->LAST_MODIFIED,true);
		$criteria->compare('MODIFIED_DATE',$this->MODIFIED_DATE,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function primaryKey() {
		return array('F_PARM');
	}

	
	
	public static function getCountry()
	{
		$sql = "select Country, CountryCode from tblCountry";
		$command = Yii::app()->db->createCommand($sql);
		$data = $command->queryAll();
		
		if($data === false)
		{
			throw new CHttpException(403, "Data not exists!");
		}
		
		return $data;
	}
	
	/**
	 * Make desc_tel from country_code and phone_number before validate
	 * @see framework/base/CModel::beforeValidate()
	 */
	public function beforeValidate()
	{
		$user = Yii::app()->user;
		//echo $this->country_code.$this->phone_number; die;
		$this->F_NUMBER = $this->country_code.$this->phone_number;
		$this->MAIN_NUMBER = $user->getState('accountNumber'); 
		return parent::beforeValidate();
	}
	
	public function uniqueSpeedDial($attribute, $params)
	{
		$user= Yii::app()->user;
		if(!$this->F_PARM)
		{
			throw new CHttpException(403,"Speed dial don't exists!");
		}
		$sql = "select F_PARM from sub_feature_parm where MAIN_NUMBER = :MAIN_NUMBER and F_PARM = :F_PARM";
		
		$command = Yii::app()->enticedb->createCommand($sql);
		$command->bindValue(":MAIN_NUMBER", $user->getState('accountNumber'));
		$command->bindValue(":F_PARM", $this->F_PARM);
		
		$data = $command->queryScalar();
		if($data)
		{
			$this->addError('uniqueSpeedDial', 'SpeedDial has taken away');
		}
	}
	
	/**
	 * methodDescription.
	 *
	 * @return returnType returnDescription
	 */
	public function bindData() {
	    if($this->F_NUMBER === NULL)
	    {
	    	throw new CHttpException(403,"call this function when dest_tel !NULL");
	    }
	    
	    //tách dử liệu
	    $arr = $this->divideData();
	    $this->country_code =  $arr[0];
	    $this->phone_number = $arr[1];
	}
	
	private function divideData()
	{
		$return = array();
		$arrCountryCode = array('1','44', '84');
	    foreach($arrCountryCode as $i => $v)
	    {
	    	if(preg_match( '/^'.$v.'/', $this->F_NUMBER) !==0)
	    	{
	    		$return[0] = trim($v);
	    		$return[1] = trim(substr($this->F_NUMBER, strlen($v)));
	    		break;
	    	}
	    }
	    
	    return $return;
	}
}